package com.lanou3g.dllo.baidumusic.main.playerdetail;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseActivity;
import com.lanou3g.dllo.baidumusic.event.ProgressEvent;
import com.lanou3g.dllo.baidumusic.main.playerdetail.lyrics.LrcFragment;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlayMusicService;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlayOrPause;
import com.lanou3g.dllo.baidumusic.main.playmusic.SongPlayBean;
import com.lanou3g.dllo.baidumusic.tools.PlaylistManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * Created by dllo on 16/7/1.
 */
public class PlayerDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView close;
    private ViewPager viewPager;
    private PlayerDetailAdapter adapter;
    private ArrayList<Fragment> datas;
    private ImageView changePictureIv;
    private TextView playMusicSongsName, playMusicSingerName;
    private SingerPhotoFragment singerPhotoFragment = new SingerPhotoFragment();
    private CheckBox pause;
    private PlayMusicService.MyBinder myBinder;
    private ServiceConnection serviceConnection;
    private int pattern;
    private ImageView playPattern;
    private SeekBar seekbar;
    private TextView runtimeStart,runtimeStop;


    @Override
    public int setLayout() {
        EventBus.getDefault().register(this);
        return R.layout.activity_playmusic;
    }

    @Override
    public void initView() {
        runtimeStop = (TextView) findViewById(R.id.playmusic_runtime_stop);
        runtimeStart = (TextView) findViewById(R.id.playmusic_runtime_start);
        seekbar = (SeekBar) findViewById(R.id.playmusic_seekbar);
        playPattern = (ImageView) findViewById(R.id.playmusic_play_order_iv);
        playPattern.setOnClickListener(this);
        findViewById(R.id.playmusic_beforesong_iv).setOnClickListener(this);
        findViewById(R.id.playmusic_nextsong).setOnClickListener(this);
        // 暂停/播放按钮
        pause = (CheckBox) findViewById(R.id.playmusic_playorpause);
        pause.setChecked(PlaylistManager.getInstance().isPlaying);
        pause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                pause.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myBinder.playPause();
                    }
                });
            }
        });
        playMusicSongsName = (TextView) findViewById(R.id.playmusic_songs_name);
        playMusicSingerName = (TextView) findViewById(R.id.playmusic_singer_name);
        adapter = new PlayerDetailAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.playmusic_viewpager);
        close = (ImageView) findViewById(R.id.playmusic_return_iv);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        changePictureIv = (ImageView) findViewById(R.id.changepicture_iv);
        changePictureIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewPager.getCurrentItem() == 0) {
                    viewPager.setCurrentItem(1);
                    changePictureIv.setImageResource(R.mipmap.tupian);
                } else {
                    viewPager.setCurrentItem(0);
                    changePictureIv.setImageResource(R.mipmap.geci);
                }

            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0) {
                    changePictureIv.setImageResource(R.mipmap.tupian);
                } else {
                    changePictureIv.setImageResource(R.mipmap.geci);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // seekBar设置监听
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                myBinder.setProgress(seekBar.getProgress());
            }
        });

    }

    @Override
    public void initData() {

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myBinder = (PlayMusicService.MyBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent playMusicService = new Intent(this, PlayMusicService.class);
        startService(playMusicService);
        bindService(playMusicService, serviceConnection, BIND_AUTO_CREATE);

        datas = new ArrayList<>();

        datas.add(singerPhotoFragment);
        datas.add(new LrcFragment());

        adapter.setFragments(datas);
        viewPager.setAdapter(adapter);
        // 获得mainActivity 传过来的歌手,歌名,图片
        PlayDetailBean playDetailBean = (PlayDetailBean) getIntent().getSerializableExtra("playDetailBean");
        if (playDetailBean != null) {
            playMusicSongsName.setText(playDetailBean.getSongName());
            playMusicSingerName.setText(playDetailBean.getSinger());
        }
        // 初始化播放模式图标
        pattern = getIntent().getIntExtra("pattern",0);
        switch (pattern) {
            case 0:
                playPattern.setImageResource(R.mipmap.liebiao);
                break;
            case 1:
                playPattern.setImageResource(R.mipmap.danqu);
                break;
            case 2:
                playPattern.setImageResource(R.mipmap.suiji);
                break;
            case 3:
                playPattern.setImageResource(R.mipmap.shunxu);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 上一曲
            case R.id.playmusic_beforesong_iv:
                myBinder.playPre();
                break;
            // 下一曲
            case R.id.playmusic_nextsong:
                if (PlaylistManager.getInstance().getPlaylist().size() == 0) {
                    myBinder.playStop();
                } else {
                    myBinder.playNext(pattern);
                }
                break;
            case R.id.playmusic_play_order_iv:
                pattern++;
                if (pattern > 3) {
                    pattern = 0;
                }
                myBinder.setPattern(pattern);
                switch (pattern) {
                    case 0:
                        playPattern.setImageResource(R.mipmap.liebiao);
                        Toast.makeText(this, "列表循环", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        playPattern.setImageResource(R.mipmap.danqu);
                        Toast.makeText(this, "单曲循环", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        playPattern.setImageResource(R.mipmap.suiji);
                        Toast.makeText(this, "随机播放", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        playPattern.setImageResource(R.mipmap.shunxu);
                        Toast.makeText(this, "顺序播放", Toast.LENGTH_SHORT).show();
                        break;
                }
        }
    }

    // 接收服务post过来的进度和播放时间
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setProgress(ProgressEvent progressEvent) {
        seekbar.setMax((int) myBinder.getDuration());
        seekbar.setProgress(progressEvent.getProgress());
        // 播放时间
        runtimeStart.setText(progressEvent.getPlayTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        // 歌曲持续时间
        runtimeStop.setText(simpleDateFormat.format(myBinder.getDuration()));
    }

    // 接收服务里发出的PlayOrPause
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void controlPlayOrPause(PlayOrPause playOrPause) {
        pause.setChecked(playOrPause.isPlaying());
    }

    // 持续获得服务里发出的歌手名和歌曲名
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getNameAndPhoto(SongPlayBean songPlayBean) {
        playMusicSongsName.setText(songPlayBean.getSonginfo().getTitle());
        playMusicSingerName.setText(songPlayBean.getSonginfo().getAuthor());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbindService(serviceConnection);
    }


}
